# Maintainer: Caleb Maclennan <caleb@alerque.com>
# Contributor: Ronald van Haren <ronald.archlinux.org>
# Contributor: Gaetan Bisson <bisson@archlinux.org>
# Contributor: Jaroslav Lichtblau <dragonlord@aur.archlinux.org>
# Contributor: Anton Leontiev <bunder@t-25.ru>

pkgname=graphicsmagick
pkgver=1.3.42
pkgrel=1
pkgdesc='Image processing system'
url='http://www.graphicsmagick.org/'
arch=(loong64 x86_64)
license=(MIT)
depends=(bzip2
         freetype2
         lcms2
         libltdl
         libpng
         libsm
         libtiff
         libwebp
         libxext
         xz)
makedepends=(jasper
             libheif
             libjxl
             libwmf
             libxml2
             perl)
optdepends=('jasper: jp2 module'
            'libwmf: wmf module'
            'libxml2: msl, svg, url modules'
            'ghostscript: pdf, ps modules'
            'libheif: heic module'
            'libjxl: jpeg-xl module')
provides=(Magick.so
          libGraphicsMagickWand.so
          libGraphicsMagick.so
          libGraphicsMagick++-Q16.so)
_archive="GraphicsMagick-$pkgver"
source=("https://downloads.sourceforge.net/project/$pkgname/$pkgname/$pkgver/$_archive.tar.xz")
sha256sums=('484fccfd2b2faf6c2ba9151469ece5072bcb91ba4ed73e75ed3d8e46c759d557')

options=('!emptydirs' 'libtool')

prepare() {
	cd "$_archive"
	sed -e "s:freetype_config='':freetype_config='/usr/bin/pkg-config freetype2':g" -i configure
}

build() {
	cd "$_archive"
	./configure \
		--prefix=/usr \
		--enable-shared \
		--with-modules \
		--with-perl \
		--with-quantum-depth=16 \
		--with-magick_plus_plus \
		--with-threads
	make
}

package() {
	cd "$_archive"
	make DESTDIR="$pkgdir" install
	install -Dm644 -t "$pkgdir/usr/share/licenses/$pkgname/" Copyright.txt
	# Install perl bindings
	# The patching was introduced in order to build perl module without installing package itself and
	# not to introduce unnecessary path into LD_RUN_PATH
	cd PerlMagick
	sed -i -e "s:'LDDLFLAGS'  => \"\(.*\)\":'LDDLFLAGS'  => \"-L${pkgdir}/usr/lib \1\":" Makefile.PL
	perl Makefile.PL INSTALLDIRS=vendor PREFIX=/usr DESTDIR="${pkgdir}"
	sed -i -e "s/LDLOADLIBS =/LDLOADLIBS = -lGraphicsMagick/" Makefile
	make
	make install
	# Remove perllocal.pod and .packlist
	rm -r "$pkgdir"/usr/lib/perl5/*/core_perl
	rm "$pkgdir"/usr/lib/perl5/*/vendor_perl/auto/Graphics/Magick/.packlist
}
